<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginForms">
          <h1>Hello</h1>
          <h2>登入就完事了</h2>
          <el-form-item prop="username">
            <el-input type="text" :prefix-icon="User" v-model="loginForm.username" :rules="rules"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" :prefix-icon="Lock" v-model="loginForm.password" show-password></el-input>
          </el-form-item>
          <el-button @click="login" :loading="loading">登录</el-button>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import useUserStore from '../../store/modules/user.ts'
import { useRouter,useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '../../utils/gettime'

let loginForms = ref()
let useStore = useUserStore()
let $router = useRouter()
let $route=useRoute()
let loading = ref()
let loginForm = reactive({
  username: 'admin',
  password: '111111',
})
const validatorname = (rule: any, value: any, callback: any) => {
  if (/^\w{5,10}$/.test(value)) {
    callback()
  } else {
    callback(new Error('账号格式错误，请输入5到10位字母或数字'))
  }
}
const validatorpassword = (rule: any, value: any, callback: any) => {
  if (value.length > 5 && value.length < 13) {
    callback()
  } else {
    callback(new Error('密码格式错误，请输入6~12位密码'))
  }
}
const rules = {
  username: [{ validator: validatorname, trigger: 'change' }],
  password: [{ validator: validatorpassword, trigger: 'change' }],

  // username: [
  //     { required: true, message: "用户名不能为空", trigger: "blur" },
  //     { required: true, min: 3, max: 8, message: "用户名长度为3到8位", trigger: "change" },
  // ],
  // password: [{ required: true, message: "密码不能为空", trigger: "blur" },
  // { required: true, min: 6, max: 12, message: "密码长度为6到12位", trigger: "change" },
  // ]
}

const login = async () => {
  await loginForms.value.validate()
  try {
    loading.value = true
    await useStore.userLogin(loginForm)
    let redirect:any=$route.query.redirect
    $router.push({path:redirect||'/'})
    ElNotification({
      type: 'success',
      message: '登录成功',
      title: `Hi,${getTime()}`,
    })
  } catch (error) {
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background-image: url('../../assets/images/background.jpg');
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background-image: url('../../assets/images/login_form.png') no-repeat;
    background-size: cover;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0px;
    }

    button {
      width: 100%;
      background-color: skyblue;
    }
  }
}
</style>
